import {request} from '../utils/request';
import {flow, makeAutoObservable, observable, action} from 'mobx';
import Loading from '../components/widget/Loading';
/**
 * user store 全局存储
 */
class MineStore {
  accountInfo: AccountInfo = {} as AccountInfo;
  collectionList: ArticleSimple[] = [];
  favoriteList: ArticleSimple[] = [];
  noteList: ArticleSimple[] = [];
  tabIndex: number = 0;
  constructor() {
    makeAutoObservable(this, {
      accountInfo: observable,
      collectionList: observable,
      favoriteList: observable,
      noteList: observable,
      tabIndex: observable,
      requestAccountInfo: action,
      requestCollectionList: action,
      requestFavoriteList: action,
      requestNoteList: action,
      setTabIndex: action,
    });
  }

  setTabIndex = (tabIndex: number) => {
    this.tabIndex = tabIndex;
  };

  requestAccountInfo = flow(function* (
    this: MineStore, // 方便使用this指针
  ) {
    Loading.show();
    try {
      const {data} = yield request('accountInfo', {});
      this.accountInfo = data;
    } catch (error) {
      console.log(error);
    } finally {
      Loading.hide();
    }
  });

  requestNoteList = flow(function* (
    this: MineStore, // 方便使用this指针
  ) {
    Loading.show();
    try {
      const {data} = yield request('noteList', {});
      this.noteList = data;
    } catch (error) {
      console.log(error);
    } finally {
      Loading.hide();
    }
  });

  requestCollectionList = flow(function* (
    this: MineStore, // 方便使用this指针
  ) {
    Loading.show();
    try {
      const {data} = yield request('collectionList', {});
      this.collectionList = data;
    } catch (error) {
      console.log(error);
    } finally {
      Loading.hide();
    }
  });

  requestFavoriteList = flow(function* (
    this: MineStore, // 方便使用this指针
  ) {
    Loading.show();
    try {
      const {data} = yield request('favorateList', {});
      this.favoriteList = data;
    } catch (error) {
      console.log(error);
    } finally {
      Loading.hide();
    }
  });
}

/**
 * ESM单例导出
 */
export default new MineStore();
